package com.helloae.chap05;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.ITableProxy;
import com.esri.arcgis.geodatabaseui.Calculator;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 使用Calculator完成ArcMap中的Field Calculator的功能。<br>
 * 示例章节：5.5.1
 * @author zhushijie
 *
 */
public class CalculateField {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap05", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("idcounty");
		
		//update是ITable定义的方法，但不是IFetureClass的方法，故必须向上转型到ITable
		ICursor pCursor = new ITableProxy(pFeatureClass).update(null, true);
		
		Calculator pCalculator = new Calculator();
		pCalculator.setCursorByRef(pCursor);
		pCalculator.setExpression("[IDCOUNTY_] + [IDCOUNTY_I]");
		pCalculator.setField("Class");
		pCalculator.calculate();
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
